package com.xzj.admin.analysis.base.custom.it;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.custom.common.CommonAnalysis;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;

/**
 * @program: xzj-admin
 * @ClassName: VsuiteAnalysisHandle
 * @description: IT云中心
 * @author: XiaZunJun
 * @create: 2024-11-12 09:07
 */
public class VSuiteAnalysisHandle extends CommonAbstractAnalysisHandle {
    static {
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.region.name(), CommonAnalysis::regionSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.host.name(), VSuiteAnalysisHandle::hostSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.storage.name(), VSuiteAnalysisHandle::storageSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.volume.name(), VSuiteAnalysisHandle::volumeSub);
    }

    public VSuiteAnalysisHandle(String cloudId,String cloudType) {
        super(cloudId, cloudType,new VSuiteThemeSpecialAnalysisHandle(cloudId,cloudType));
    }

    @Override
    public void analysisBefore(JSONObject msgJson) {
        JSONObject extendJson = msgJson.getJSONObject(CommonEnums.JSON_FIELD.EXTEND.getValue());
        if (extendJson.containsKey("extendParentId")) {
            for (String key : msgJson.keySet()) {
                if (CommonEnums.JSON_FIELD.EXTEND.judgeEquals(key)) {
                    JSONObject themeJson = msgJson.getJSONObject(key);
                    themeJson.put("extendParentId", extendJson.getString("extendParentId"));
                    msgJson.put(key, themeJson);
                }
            }
        }
    }

    /**
     * 构建依托于主机 主机与集群关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void hostSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        String extendParentId = row.getString(fieldName);
        if (StrUtil.isNotEmpty(extendParentId)) {
            JSONObject subJson = new JSONObject();
            subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, extendParentId));
            subJson.put("hostId", parentId);
            subJson.put("clusterId", extendParentId);
            subThemeRows.add(subJson);
        }
    }

    /**
     * 构建依托于存储 主机与虚拟存储关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void storageSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        String[] splitField = fieldName.split(CommonEnums.JOIN.DEFAULT.getValue());
        String hostId = row.getJSONObject(splitField[0]).getString(splitField[1]);
        if (StrUtil.isNotEmpty(hostId)) {
            JSONObject subJson = new JSONObject();
            subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, hostId));
            subJson.put("hostId", hostId);
            subJson.put("storageId", parentId);
            subJson.put("specId", "12580002");
            subThemeRows.add(subJson);
        }
    }

    /**
     * 构建依托于云磁盘 磁盘与虚机关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void volumeSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        String extendParentId = row.getString(fieldName);
        if (StrUtil.isNotEmpty(extendParentId)) {
            JSONObject subJson = new JSONObject();
            subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, extendParentId));
            subJson.put("vmId", extendParentId);
            subJson.put("volumeId", parentId);
            subThemeRows.add(subJson);
        }
    }

}
